package guiPackage;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import adapterPackage.LimanAdapter;
import adapterPackage.MusteriAdapter;
import adapterPackage.SeferAdapter;

import systemPackage.LocalStrings;
import systemPackage.MainSystem;

import dataTypesPackage.DistanceBetweenLiman;
import dataTypesPackage.Liman;
import dataTypesPackage.Musteri;
import dataTypesPackage.Sefer;



public class GUI_MainWindow extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4094441335096928479L;
	
	private JButton addNewMusteriButton = new JButton(LocalStrings.getLocalString(4));
	private JButton editMusteriButton = new JButton(LocalStrings.getLocalString(7));
	private JButton listMusteriButton = new JButton(LocalStrings.getLocalString(9));
	public JLabel lastTransaction = new JLabel();
	private JButton addNewLimanButton= new JButton(LocalStrings.getLocalString(10));
	private JButton listLimanButton = new JButton(LocalStrings.getLocalString(18));
	private JButton createNewVoyage = new JButton(LocalStrings.getLocalString(22));
	private JButton listVoyageButton = new JButton(LocalStrings.getLocalString(28));
	private JButton makeSaleButton = new JButton(LocalStrings.getLocalString(35));
	
	public GUI_MainWindow() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		createGUI();
	}

	private void createGUI() {
		// Main window creater method.
		Container container = getContentPane();
		container.setLayout(new GridLayout(9,1));
		ActionListener addNewMusteriButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method triggers newMusteri GUI
				GUI_NewMusteri GUIsaveMusteri = new GUI_NewMusteri();
				GUIsaveMusteri.setVisible(true);
			}
		};
		addNewMusteriButton.addActionListener(addNewMusteriButtonPress);

		ActionListener editMusteriButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method triggers newMusteri GUI
				GUI_editMusteri GUIEditMusteri = new GUI_editMusteri();
				GUIEditMusteri.setVisible(true);
			}
		};
		editMusteriButton.addActionListener(editMusteriButtonPress);
		
		ActionListener ListMusteriButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method triggers newMusteri GUI
				//as all getAll functions, we have to cast to according class
				List<Musteri> musteriList = MusteriAdapter.getAllMusteri();
				// TODO implement this
				for(int i = 0; i<musteriList.size();i++){
					System.out.print(musteriList.get(i).getMusteriID()+"\t");
					System.out.print(musteriList.get(i).getName()+"\t");
					System.out.println(musteriList.get(i).getSurName());
					
				}
			}
		};
		listMusteriButton.addActionListener(ListMusteriButtonPress);
		
        ActionListener addNewLimanButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method triggers newLimanGUI
				GUI_NewLiman GUIsaveLiman = new GUI_NewLiman();
				GUIsaveLiman.setVisible(true);
			}
		};
	
		addNewLimanButton.addActionListener(addNewLimanButtonPress);
		
		ActionListener ListLimanButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method triggers newMusteri GUI

				List<Liman> limanList = LimanAdapter.getAllLiman();
				// TOD0 implement this

				for(int i = 0; i<limanList.size();i++){
					System.out.print("id : " + limanList.get(i).getLimanID()+"\t");
					System.out.print(" name : " + limanList.get(i).getName() +"\t");
					System.out.println(" city : " + limanList.get(i).getCity() +"\n" );
					
				}
				System.out.println("distances :");
				List<DistanceBetweenLiman> distanceBetweenLimans = MainSystem.getAllDistances();
				for(int i = 0; i<distanceBetweenLimans.size();i++){
					System.out.print("from : " + distanceBetweenLimans.get(i).getLimanFrom().getName() +"\t");
					System.out.print(" to : " + distanceBetweenLimans.get(i).getLimanTo().getName() +"\t");
					System.out.println(" distance : " + distanceBetweenLimans.get(i).getDistance() +"\n" );
					
				}
			}
		};
		listLimanButton.addActionListener(ListLimanButtonPress);
		
        ActionListener createNewVoyagePress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method triggers newLimanGUI
				GUI_NewSefer GUInewSefer = new GUI_NewSefer();
				GUInewSefer.setVisible(true);
			}
		};
	
		createNewVoyage.addActionListener(createNewVoyagePress);
		
		ActionListener ListVoyageButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method triggers newMusteri GUI
				List<Sefer> seferList = SeferAdapter.getAllSefer();
				// TOD0 implement this
				for(int i = 1; i<seferList.size();i++){
					System.out.print("name : " + seferList.get(i).getName()+"\t");
					System.out.print(" gemi : " + seferList.get(i).getGemi() +"\t");
					System.out.println(" guzergah : " + seferList.get(i).getGuzergah() +"\n" );
				
					System.out.println("prices :");
					for(int j = 0; j<seferList.get(i).getFiyatlar().size();j++){
						System.out.print("type : " + Musteri.MusteriTypes.values()[j] +"\t");
						System.out.println(" price : " + seferList.get(i).getFiyatlar().get(j) +"\n" );
					}
				}
			}
		};
		listVoyageButton.addActionListener(ListVoyageButtonPress);
		
        ActionListener makeSalePress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// This method triggers newLimanGUI
				GUI_NewSatis GUInewSatis = new GUI_NewSatis();
				GUInewSatis.setVisible(true);
			}
		};
	
		makeSaleButton.addActionListener(makeSalePress);
		
		container.add(addNewMusteriButton);
		container.add(editMusteriButton);
		container.add(listMusteriButton);
		container.add(addNewLimanButton);
		container.add(listLimanButton);
		container.add(createNewVoyage);
		container.add(listVoyageButton);
		container.add(makeSaleButton);
		
		container.add(lastTransaction);
		
		this.setSize(240, 300);
	}

	public void updateLastTransaction(String lastTransactionString) {
		// Updates last transaction text
		this.lastTransaction.setText(lastTransactionString);
		this.repaint();
		
	}
		
	
}
